package com.shenmazong.serverwebpoem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shenmazong.serverwebpoem.mapper.IDbPoetryMapper;
import com.shenmazong.serverwebpoem.mapper.ITbPoetryMapper;
import com.shenmazong.serverwebpoem.pojo.TbPoetry;
import com.shenmazong.serverwebpoem.service.TbPoetryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 田不平
 * @project server-web-poem
 * @date 2020/3/23 3:00 PM
 * http://www.shenmazong.com
 */
@Slf4j
@Service
public class TbPoetryServiceImpl implements TbPoetryService {

    @Autowired
    private ITbPoetryMapper iTbPoetryMapper;

    /**
     * TODO 根据诗词的名字查询诗词
     * @param title
     * @return
     */
    @Override
    public TbPoetry getPoetryByTitle(String title) {

        return iTbPoetryMapper.selectOne(new QueryWrapper<TbPoetry>().eq("title", title));
    }

    /**
     * TODO 根据ID查询诗词
     * @param id
     * @return
     */
    @Override
    public TbPoetry getPoetryById(Integer id) {
        return iTbPoetryMapper.selectById(id);
    }

    /**
     * TODO 多条件查询诗词
     * @param poetry
     * @return
     */
    @Override
    public List<TbPoetry> getPoetry(TbPoetry poetry) {

        QueryWrapper<TbPoetry> queryWrapper = new QueryWrapper<>(poetry);
        return iTbPoetryMapper.selectList(queryWrapper);
    }

    /**
     * TODO 初始化诗词，用于初始导入数据
     * @param poetry
     */
    @Override
    public void initPoetry(TbPoetry poetry) {

        iTbPoetryMapper.insert(poetry);
    }

    /**
     * TODO 增加诗词
     * @param poetry
     */
    @Override
    public void addPoetry(TbPoetry poetry) {
        iTbPoetryMapper.insert(poetry);
    }

    /**
     * TODO 删除诗词
     * @param id
     */
    @Override
    public void delPoetry(Integer id) {
        iTbPoetryMapper.deleteById(id);
    }

    /**
     * TODO 修改诗词
     * @param poetry
     */
    @Override
    public void setPoetry(TbPoetry poetry) {
        iTbPoetryMapper.updateById(poetry);
    }

    /**
     * TODO 随机获取num条诗词
     * @param num
     * @return
     */
    @Override
    public List<TbPoetry> getRandList(Integer num) {
//        List<TbPoetry> poetries = iTbPoetryMapper.selectList(
//                new QueryWrapper<TbPoetry>()
//                        .orderByDesc("RAND()")
//                        .last("limit 5")
//        );

        List<TbPoetry> poetries = iTbPoetryMapper.selectList(
                new QueryWrapper<TbPoetry>()
                        .last("limit " + num)
        );

        return poetries;
    }

    @Override
    public List<TbPoetry> getListByAuthorId(Integer authorId) {
        List<TbPoetry> poetries = iTbPoetryMapper.selectList(
                new QueryWrapper<TbPoetry>()
                        .eq("authorid", authorId)
                        .last("limit 5")
        );

        return poetries;
    }

    /**
     * TODO 诗词分页列表
     * @param pageCurrent
     * @param pageSize
     * @return
     */
    @Override
    public IPage<TbPoetry> getListByPage(Integer pageCurrent, Integer pageSize) {

        // 设置查询条件
        QueryWrapper<TbPoetry> queryWrapper = new QueryWrapper<>();
//        queryWrapper.last("limit 0,5");

        // 第一种
        // 总页数+总记录数
        Page<TbPoetry> page = new Page<>(pageCurrent, pageSize);
        IPage<TbPoetry> iPage = iTbPoetryMapper.selectPage(page, queryWrapper);
        System.out.println("总页数:"+iPage.getPages());
        System.out.println("总记录数:"+iPage.getTotal());

        return iPage;

//        List<TbPoetry> list = iPage.getRecords();
//        return list;

        // 第二种
//        Page<TbPoetry> page = new Page<>(1, 5);
//        IPage<Map<String, Object>> iPage = iTbPoetryMapper.selectMapsPage(page, queryWrapper);
//        System.out.println("总页数:" + iPage.getPages());
//        System.out.println("总记录数:" + iPage.getTotal());
//
//        List<Map<String, Object>> poetries = iPage.getRecords();
//
//        return null;
    }

    /**
     * TODO 根据作者ID查找他的所有诗词
     * @param pageCurrent
     * @param pageSize
     * @return
     */
    @Override
    public IPage<TbPoetry> getListByAuthorId(Integer authorId, Integer pageCurrent, Integer pageSize) {

        QueryWrapper<TbPoetry> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("authorid", authorId);

        Page<TbPoetry> page = new Page<>(pageCurrent, pageSize);
        IPage<TbPoetry> iPage = iTbPoetryMapper.selectPage(page, queryWrapper);
        System.out.println("总页数:"+iPage.getPages());
        System.out.println("总记录数:"+iPage.getTotal());

        return iPage;
    }

    /**
     * TODO 获取诗词列表（含统计信息）
     * @param pageCurrent
     * @param pageSize
     * @return
     */
    @Override
    public IPage<Map<String, Object>> getMapPoetryByPage(Integer pageCurrent, Integer pageSize) {
        //QueryWrapper<TbPoetry> queryWrapper = new QueryWrapper<>();
        //queryWrapper.eq("authorid", authorId);

        Page<Map<String, Object>> page = new Page<>(pageCurrent, pageSize);
        IPage<Map<String, Object>> iPage = iTbPoetryMapper.selectMapPoetryByPage(page);
        System.out.println("总页数:"+iPage.getPages());
        System.out.println("总记录数:"+iPage.getTotal());

        return iPage;
    }

    /**
     * TODO 根据作者ID获取诗词列表（含统计信息）
     * @param authorId
     * @param pageCurrent
     * @param pageSize
     * @return
     */
    @Override
    public IPage<Map<String, Object>> getMapPoetry4AuthorByPage(Integer authorId, Integer pageCurrent, Integer pageSize) {

        Page<Map<String, Object>> page = new Page<>(pageCurrent, pageSize);
        IPage<Map<String, Object>> iPage = iTbPoetryMapper.selectMapPoetry4AuthorByPage(page, authorId);
        System.out.println("总页数:"+iPage.getPages());
        System.out.println("总记录数:"+iPage.getTotal());

        return iPage;
    }

    /**
     * TODO 根据点击率，获取诗词列表
     * @param pageCurrent
     * @param pageSize
     * @return
     */
    @Override
    public IPage<Map<String, Object>> getMapPoetry4ClickByPage(Integer pageCurrent, Integer pageSize) {

        Page<Map<String, Object>> page = new Page<>(pageCurrent, pageSize);
        IPage<Map<String, Object>> iPage = iTbPoetryMapper.selectMapPoetry4ClickByPage(page);
        System.out.println("总页数:"+iPage.getPages());
        System.out.println("总记录数:"+iPage.getTotal());

        return iPage;
    }

    /**
     * TODO 获取没有导入ES库中的诗词列表
     * @param pageCurrent
     * @param pageSize
     * @return
     */
    @Override
    public IPage<TbPoetry> getList4NotInElastic(Integer pageCurrent, Integer pageSize) {
        Page<TbPoetry> page = new Page<>(pageCurrent, pageSize);
        IPage<TbPoetry> iPage = iTbPoetryMapper.selectList4NotInElastic(page);
        return iPage;
    }

    /**
     * TODO 获取没有导入ES库中的诗词列表2
     * @return
     */
    @Override
    public List<TbPoetry> getList4EsIsNULL() {
        return iTbPoetryMapper.selectList4EsIsNULL();
    }
}
